#include "JsonParser.h"
#include "ApiBaseInfo.h"
#include "zlog.h"
void IOTA_getLocalTime(char *pcLocalTime)
{
#if defined(_WINDOWS) || defined(WIN32)
	SYSTEMTIME st;
	::GetLocalTime(&st);
	_snprintf_s(pcLocalTime, 31, 31, "%04d%02d%02dT%02d%02d%02d+8:00", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
#else
	time_t tt;
	struct tm _tm;
	tt = time(&tt);
	localtime_r(&tt, &_tm);

	// struct timeval tv;
	// struct timezone tz;
	//::gettimeofday(&tv, &tz);
	// long int limillsec = tv.tv_usec / 1000;

	snprintf(pcLocalTime, 31, ("%04d%02d%02dT%02d%02d%02d+8:00"), _tm.tm_year + 1900, _tm.tm_mon + 1, _tm.tm_mday, _tm.tm_hour, _tm.tm_min, _tm.tm_sec);
#endif

	return;
}

/**
 * @description: 返回当前时间字符串
 * @return {*} 返回char* 字符串，要记得free
 */
char *IOTA_getLocalTimeString()
{
#if defined(_WINDOWS) || defined(WIN32)
	SYSTEMTIME st;
	::GetLocalTime(&st);
	_snprintf_s(pcLocalTime, 31, 31, "%04d%02d%02dT%02d%02d%02d+8:00", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
#else
	time_t tt;
	struct tm _tm;
	tt = time(&tt);
	localtime_r(&tt, &_tm);
	char *pcLocalTime = (char *)malloc(33);
	// struct timeval tv;
	// struct timezone tz;
	//::gettimeofday(&tv, &tz);
	// long int limillsec = tv.tv_usec / 1000;

	snprintf(pcLocalTime, 31, ("%04d%02d%02dT%02d%02d%02d+8:00"), _tm.tm_year + 1900, _tm.tm_mon + 1, _tm.tm_mday, _tm.tm_hour, _tm.tm_min, _tm.tm_sec);
#endif

	return pcLocalTime;
}

bool StructToJson_DeviceAdd(unsigned int uiMid, ST_IOTA_DEVICE_INFO *stDevices, char **pcJson)
{
	cJSON *json = NULL;
	cJSON *dev = NULL;
	cJSON *infos = NULL;
	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddNumberToObject(json, "mid", uiMid);

		cJSON_AddItemToObject(json, "deviceInfos", infos = cJSON_CreateArray());
		cJSON_AddItemToArray(infos, dev = cJSON_CreateObject());
		cJSON_AddStringToObject(dev, "nodeId", stDevices->pcNodeId);
		cJSON_AddStringToObject(dev, "name", stDevices->pcName);
		cJSON_AddStringToObject(dev, "description", stDevices->pcDescription);
		cJSON_AddStringToObject(dev, "manufacturerId", stDevices->pcManufacturerId);
		cJSON_AddStringToObject(dev, "model", stDevices->pcModel);

		{
			*pcJson = cJSON_PrintUnformatted(json);
		}

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

bool StructToJson_DeviceRemove(unsigned int uiMid, char *pcDeviceId, char **pcJson)
{
	cJSON *json = NULL;
	cJSON *request = NULL;
	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddNumberToObject(json, "id", uiMid);
		cJSON_AddStringToObject(json, "deviceId", pcDeviceId);
		char t[32] = {0};
		IOTA_getLocalTime(t);
		cJSON_AddStringToObject(json, "requestTime", t);

		cJSON_AddItemToObject(json, "request", request = cJSON_CreateObject());

		{
			*pcJson = cJSON_PrintUnformatted(json);
		}

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

bool StructToJson_DeviceStatusUpdate(unsigned int uiMid, ST_IOTA_DEVICE_STATUS *stStatuss, char **pcJson)
{
	cJSON *json = NULL;
	cJSON *status = NULL;
	cJSON *statuses = NULL;
	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddNumberToObject(json, "mid", uiMid);

		cJSON_AddItemToObject(json, "deviceStatuses", statuses = cJSON_CreateArray());
		cJSON_AddItemToArray(statuses, status = cJSON_CreateObject());
		cJSON_AddStringToObject(status, "deviceId", stStatuss->pcDeviceId);
		cJSON_AddStringToObject(status, "status", stStatuss->pcStatus);

		{
			*pcJson = cJSON_PrintUnformatted(json);
		}

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

bool StructToJson_DeviceQuery(unsigned int uiMid, char *pcNodeId, char **pcJson)
{
	cJSON *json = NULL;
	cJSON *status = NULL;
	cJSON *statuses = NULL;
	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddNumberToObject(json, "mid", uiMid);
		cJSON_AddStringToObject(json, "nodeId", pcNodeId);

		{
			*pcJson = cJSON_PrintUnformatted(json);
		}

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

bool StructToJson_ServiceDataReport(char *pcDeviceId, char *pcServiceId, char *pcServiceProperties, char **pcJson)
{
	cJSON *json = NULL;
	cJSON *data = NULL;
	cJSON *service = NULL;
	cJSON *services = NULL;
	cJSON *dev = NULL;
	cJSON *devs = NULL;
	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddItemToObject(json, "devices", devs = cJSON_CreateArray());
		cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());

		cJSON_AddStringToObject(dev, "deviceId", pcDeviceId);
		cJSON_AddItemToObject(dev, "services", services = cJSON_CreateArray());
		cJSON_AddItemToArray(services, service = cJSON_CreateObject());

		cJSON_AddStringToObject(service, "serviceId", pcServiceId);
		cJSON_AddItemToObject(service, "data", data = cJSON_Parse(pcServiceProperties));

		char t[32] = {0};
		IOTA_getLocalTime(t);
		cJSON_AddStringToObject(service, "eventTime", t);

		{
			*pcJson = cJSON_PrintUnformatted(json);
		}

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

bool StructToJson_ServiceCommandResponse(unsigned int uiMid, unsigned int uiResultCode, char *pcCommandResponse, char **pcJson)
{
	cJSON *json = NULL;
	cJSON *body = NULL;
	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddNumberToObject(json, "mid", uiMid);
		cJSON_AddStringToObject(json, "msgType", "deviceRsp");
		cJSON_AddNumberToObject(json, "errcode", uiResultCode);
		cJSON_AddItemToObject(json, "body", body = cJSON_Parse(pcCommandResponse));

		{
			*pcJson = cJSON_PrintUnformatted(json);
		}

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

bool StructToJson_ServiceCustomTopicReport(ST_IOTA_BatchReportProperties *pstDevInfo, char **pcJson)
{
	cJSON *json = NULL;
	cJSON *data = NULL;
	cJSON *service = NULL;
	cJSON *services = NULL;
	cJSON *dev = NULL;
	cJSON *devs = NULL;
	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddItemToObject(json, "devices", devs = cJSON_CreateArray());
		cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());

		cJSON_AddStringToObject(dev, "deviceId", pstDevInfo->pcDeviceId);
		cJSON_AddItemToObject(dev, "services", services = cJSON_CreateArray());
		cJSON_AddItemToArray(services, service = cJSON_CreateObject());

		cJSON_AddStringToObject(service, "serviceId", pstDevInfo->pcServiceId);
		cJSON_AddItemToObject(service, "data", data = cJSON_Parse(pstDevInfo->pcPayload));

		char t[32] = {0};
		IOTA_getLocalTime(t);
		cJSON_AddStringToObject(service, "eventTime", t);

		{
			*pcJson = cJSON_PrintUnformatted(json);
		}

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

bool JsonToStruct_DeviceAddRsp(char *pcJson, unsigned int *uiMid, int *iStatusCode, char *pcStatusDecs, ST_IOTA_ADD_DEVICE_RSP *stRsp)
{
	cJSON *json = NULL;
	cJSON *dataList = NULL;
	cJSON *data = NULL;
	cJSON *dev = NULL;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		*uiMid = cJSON_GetObjectItem(json, "mid")->valueint;
		*iStatusCode = cJSON_GetObjectItem(json, "statusCode")->valueint;
		if (cJSON_HasObjectItem(json, "statusDesc"))
		{
			strcpy(pcStatusDecs, cJSON_GetObjectItem(json, "statusDesc")->valuestring);
		}
		dataList = cJSON_GetObjectItem(json, "data");
		int i = 0;
		for (i = 0; i < cJSON_GetArraySize(dataList); i++)
		{
			data = cJSON_GetArrayItem(dataList, i);
			if (cJSON_HasObjectItem(data, "deviceInfo"))
			{
				dev = cJSON_GetObjectItem(data, "deviceInfo");
				if (cJSON_HasObjectItem(dev, "description"))
				{
					strcpy(stRsp->stDeviceInfos.pcDescription, cJSON_GetObjectItem(dev, "description")->valuestring);
				}
				strcpy(stRsp->stDeviceInfos.pcDeviceId, cJSON_GetObjectItem(dev, "deviceId")->valuestring);
				strcpy(stRsp->stDeviceInfos.pcManufacturerId, cJSON_GetObjectItem(dev, "manufacturerId")->valuestring);
				strcpy(stRsp->stDeviceInfos.pcModel, cJSON_GetObjectItem(dev, "model")->valuestring);
				strcpy(stRsp->stDeviceInfos.pcName, cJSON_GetObjectItem(dev, "name")->valuestring);
				strcpy(stRsp->stDeviceInfos.pcNodeId, cJSON_GetObjectItem(dev, "nodeId")->valuestring);
			}
			if (cJSON_HasObjectItem(data, "statusDesc"))
			{
				strcpy(stRsp->pcStatusDecs, cJSON_GetObjectItem(data, "statusDesc")->valuestring);
			}
			break;
		}

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

bool JsonToStruct_DeviceStatusUpdateRsp(char *pcJson, unsigned int *uiMid, int *iStatusCode, char *pcStatusDecs, ST_IOTA_UPDATE_STATUS_RSP *stRsp)
{
	cJSON *json = NULL;
	cJSON *dataList = NULL;
	cJSON *data = NULL;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		*uiMid = cJSON_GetObjectItem(json, "mid")->valueint;
		*iStatusCode = cJSON_GetObjectItem(json, "statusCode")->valueint;
		if (cJSON_HasObjectItem(json, "statusDesc"))
		{
			strcpy(pcStatusDecs, cJSON_GetObjectItem(json, "statusDesc")->valuestring);
		}
		dataList = cJSON_GetObjectItem(json, "data");
		int i = 0;
		for (i = 0; i < cJSON_GetArraySize(dataList); i++)
		{
			data = cJSON_GetArrayItem(dataList, i);
			strcpy(stRsp->pcDeviceId, cJSON_GetObjectItem(data, "deviceId")->valuestring);

			if (cJSON_HasObjectItem(data, "statusDesc"))
			{
				strcpy(stRsp->pcStatusDecs, cJSON_GetObjectItem(data, "statusDesc")->valuestring);
			}
			break;
		}

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

bool JsonToStruct_DeviceQueryRsp(char *pcJson, unsigned int *uiMid, int *iStatusCode, char *pcStatusDecs, ST_IOTA_DEVICE_INFO *stRsp, char *pcCount, char *pcMarker)
{
	cJSON *json = NULL;
	cJSON *dataList = NULL;
	cJSON *data = NULL;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		*uiMid = cJSON_GetObjectItem(json, "mid")->valueint;
		*iStatusCode = cJSON_GetObjectItem(json, "statusCode")->valueint;
		if (cJSON_HasObjectItem(json, "statusDesc"))
		{
			strcpy(pcStatusDecs, cJSON_GetObjectItem(json, "statusDesc")->valuestring);
		}
		dataList = cJSON_GetObjectItem(json, "data");
		int i = 0;
		for (i = 0; i < cJSON_GetArraySize(dataList); i++)
		{
			data = cJSON_GetArrayItem(dataList, i);
			strcpy(stRsp->pcDeviceId, cJSON_GetObjectItem(data, "deviceId")->valuestring);
			strcpy(stRsp->pcNodeId, cJSON_GetObjectItem(data, "nodeId")->valuestring);
			strcpy(stRsp->pcName, cJSON_GetObjectItem(data, "name")->valuestring);
			strcpy(stRsp->pcManufacturerId, cJSON_GetObjectItem(data, "manufactureId")->valuestring);
			strcpy(stRsp->pcModel, cJSON_GetObjectItem(data, "model")->valuestring);

			if (cJSON_HasObjectItem(data, "description"))
			{
				strcpy(stRsp->pcDescription, cJSON_GetObjectItem(data, "description")->valuestring);
			}
			break;
		}
		if (cJSON_HasObjectItem(json, "count"))
		{
			strcpy(pcCount, cJSON_GetObjectItem(json, "count")->valuestring);
		}
		if (cJSON_HasObjectItem(json, "marker"))
		{
			strcpy(pcMarker, cJSON_GetObjectItem(json, "marker")->valuestring);
		}

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

bool JsonToStruct_Command(char *pcJson, ST_IOTA_COMMAND *stCommand)
{
	cJSON *json = NULL;
	cJSON *params = NULL;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		stCommand->iMid = cJSON_GetObjectItem(json, "mid")->valueint;
		strcpy(stCommand->pcDeviceId, cJSON_GetObjectItem(json, "deviceId")->valuestring);
		strcpy(stCommand->pcMsgType, cJSON_GetObjectItem(json, "msgType")->valuestring);
		strcpy(stCommand->pcServiceId, cJSON_GetObjectItem(json, "serviceId")->valuestring);
		strcpy(stCommand->pcCmd, cJSON_GetObjectItem(json, "cmd")->valuestring);
		params = cJSON_GetObjectItem(json, "paras");

		char *s = cJSON_PrintUnformatted(params);
		if (s)
		{
			strcpy(stCommand->pcParas, s);
			free(s);
		}

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

bool JsonToStruct_Response(char *pcJson, unsigned int *uiMid, int *iStatusCode, char *pcStatusDecs)
{
	cJSON *json = NULL;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		*uiMid = cJSON_GetObjectItem(json, "mid")->valueint;
		*iStatusCode = cJSON_GetObjectItem(json, "statusCode")->valueint;
		if (cJSON_HasObjectItem(json, "statusDesc"))
		{
			pcStatusDecs = strdup(cJSON_GetObjectItem(json, "statusDesc")->valuestring);
		}

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

/**
 * @description: 日志上传结构体
 * @param {char} *pcJson 上传要转换json
 * @param {ST_IOTA_LOG} *pcStatusDecs 转换后的结构体
 * @return {*}
 */
bool JsonToStruct_LogUpload(char *pcJson, ST_IOTA_LOG *pcStatusDecs)
{
	cJSON *json = NULL;
	cJSON *param = NULL;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		pcStatusDecs->iMid = strdup(cJSON_GetObjectItem(json, "mid")->valuestring);
		pcStatusDecs->deviceSn = strdup(cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		pcStatusDecs->Servicename = strdup(cJSON_GetObjectItem(json, "service")->valuestring);
		pcStatusDecs->zone = strdup(cJSON_GetObjectItem(json, "zone")->valuestring);
		pcStatusDecs->node = strdup(cJSON_GetObjectItem(json, "node")->valuestring);

		if (cJSON_HasObjectItem(json, "requestId") && strlen(cJSON_GetObjectItem(json, "requestId")->valuestring) > 0)
		{
			pcStatusDecs->requestID = strdup(cJSON_GetObjectItem(json, "requestId")->valuestring);
		}
		else
		{
			pcStatusDecs->requestID = strdup("com.jxtech.iot_ops - tasks");
		}
		pcStatusDecs->msgTime = strdup(cJSON_GetObjectItem(json, "msgTime")->valuestring);
		pcStatusDecs->deviceID = strdup(cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		pcStatusDecs->cmd = strdup(cJSON_GetObjectItem(json, "cmd")->valuestring);

		if (cJSON_HasObjectItem(json, "params"))
		{
			param = cJSON_GetObjectItem(json, "params");
			ST_IOTA_LOG_PARAM *st_param = pcStatusDecs->param;
			st_param->logPath = strdup(cJSON_GetObjectItem(param, "logPath")->valuestring);
			st_param->beginTime = strdup(cJSON_GetObjectItem(param, "beginTime")->valuestring);
			st_param->endTime = strdup(cJSON_GetObjectItem(param, "endTime")->valuestring);
		}

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

/**
 * @description: 结构体转换，日志上传
 * @param {char} * 传出json
 * @param {ST_IOTA_LOG_RES} *pcStatusDecs 传入结构体
 * @return {*} 是否成功
 */
bool StructToJson_LogUploadRes(char **pcJson, ST_IOTA_LOG_RES *pcStatusDecs, bool isstring)
{
	if (pcStatusDecs)
	{
		cJSON *json = NULL;
		json = cJSON_CreateObject();
		char cTime[128] = {0};
		int ret = systemDate4(cTime);
		cJSON_AddStringToObject(json, "mid", pcStatusDecs->iMid);
		cJSON_AddNumberToObject(json, "code", pcStatusDecs->code);
		cJSON_AddStringToObject(json, "deviceSn", pcStatusDecs->deviceSn);
		cJSON_AddStringToObject(json, "replyId", pcStatusDecs->replyId);
		cJSON_AddStringToObject(json, "zone", pcStatusDecs->zone);
		cJSON_AddStringToObject(json, "node", pcStatusDecs->node);
		cJSON_AddStringToObject(json, "msgTime", cTime);
		cJSON_AddStringToObject(json, "deviceId", pcStatusDecs->deviceID);

		cJSON_AddNumberToObject(json, "deviceType", pcStatusDecs->deviceType);

		cJSON_AddStringToObject(json, "subDeviceId", "");//child devices not influence other
		if (pcStatusDecs->requestID != NULL)
		{
			cJSON_AddStringToObject(json, "requestId", pcStatusDecs->requestID);
		}
		// body
		ST_IOTA_LOG_BODY *st_body = (ST_IOTA_LOG_BODY *)pcStatusDecs->body;
		if (st_body && isstring == false)
		{
			cJSON *body = NULL;
			body = cJSON_CreateObject();
			cJSON_AddStringToObject(body, "filename", st_body->FileName);
			cJSON_AddStringToObject(body, "fileId", st_body->filed);
			cJSON_AddStringToObject(body, "md5", st_body->md5);
			cJSON_AddNumberToObject(body, "size", st_body->size);
			cJSON_AddItemToObject(json, "body", body);
		}
		else if (st_body && isstring == true)
		{
			//	cJSON_AddStringToObject(json, "body", st_body);
		}
		*pcJson = cJSON_PrintUnformatted(json);

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

/**
 * @description: 日志规则下发
 * @param {char} *pcJson
 * @param {ST_IOTA_LOG} *pcStatusDecs
 * @return {*}
 */
bool JsonToStruct_LogRule(char *pcJson, ST_IOTA_LOG *pcStatusDecs)
{
	cJSON *json = NULL;
	cJSON *param = NULL;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		pcStatusDecs->iMid = strdup(cJSON_GetObjectItem(json, "mid")->valuestring);
		pcStatusDecs->deviceSn = strdup(cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		pcStatusDecs->Servicename = strdup(cJSON_GetObjectItem(json, "Servicename")->valuestring);
		pcStatusDecs->zone = strdup(cJSON_GetObjectItem(json, "zone")->valuestring);
		pcStatusDecs->node = strdup(cJSON_GetObjectItem(json, "node")->valuestring);
		pcStatusDecs->requestID = strdup(cJSON_GetObjectItem(json, "requestId")->valuestring);
		pcStatusDecs->msgTime = strdup(cJSON_GetObjectItem(json, "msgTime")->valuestring);
		pcStatusDecs->deviceID = strdup(cJSON_GetObjectItem(json, "deviceSn")->valuestring);
		pcStatusDecs->cmd = strdup(cJSON_GetObjectItem(json, "cmd")->valuestring);

		if (cJSON_HasObjectItem(json, "param"))
		{
			param = cJSON_GetObjectItem(json, "param");
			ST_IOTA_LOG_RULE_PARAM *st_param = pcStatusDecs->param;
			st_param->path = strdup(cJSON_GetObjectItem(param, "path")->valuestring);
			st_param->regex = strdup(cJSON_GetObjectItem(param, "regex")->valuestring);
		}

		cJSON_Delete(json);
		cJSON_Delete(param);

		return true;
	}
	else
	{
		return false;
	}
}

/**
 * @description: 日志规则下发回执
 * @param {char} *
 * @param {ST_IOTA_LOG_RES} *pcStatusDecs
 * @return {*}
 */
bool StructToJson_LogRuleRes(char *pcJson, ST_IOTA_LOG_RES *pcStatusDecs)
{
	if (pcStatusDecs)
	{
		cJSON *json = NULL;
		cJSON *body = NULL;
		json = cJSON_CreateObject();
		body = cJSON_CreateObject();
		char *time = IOTA_getLocalTimeString();
		// json
		cJSON_AddStringToObject(json, "mid", pcStatusDecs->iMid);
		cJSON_AddNumberToObject(json, "code", pcStatusDecs->code);
		cJSON_AddStringToObject(json, "deviceSn", pcStatusDecs->deviceSn);
		cJSON_AddStringToObject(json, "replyId", pcStatusDecs->replyId);
		cJSON_AddStringToObject(json, "msgTime", time);

		*pcJson = cJSON_PrintUnformatted(json);

		free(time);
		cJSON_Delete(body);
		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}
/**
 * @description: 日志登录
 * @param {char} *pcJson
 * @param {ST_IOTA_LOGIN_RES} *pcStatusDecs
 * @return {*}
 */
bool JsonToStruct_Login(char *pcJson, ST_IOTA_LOGIN_RES_V1 *pcStatusDecs)
{
	cJSON *json = NULL;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		pcStatusDecs->resultCode = cJSON_GetObjectItem(json, "resultCode")->valueint;
		pcStatusDecs->message = strdup(cJSON_GetObjectItem(json, "message")->valuestring);
		if (pcStatusDecs->resultCode < 400) // 服务端返回500时，data为null
		{
			cJSON *data = cJSON_GetObjectItem(json, "data");
			ST_IOTA_AUTH_RES_V1 *tmp = pcStatusDecs->data;

			tmp->nonce = strdup(cJSON_GetObjectItem(data, "nonce")->valuestring);
			tmp->timestamp = strdup(cJSON_GetObjectItem(data, "timestamp")->valuestring);
			tmp->token = strdup(cJSON_GetObjectItem(data, "token")->valuestring);
		}
		if (cJSON_HasObjectItem(json, "timeStamp"))
		{
			pcStatusDecs->timeStamp = cJSON_GetObjectItem(json, "timeStamp")->valuedouble;
		}

		cJSON_Delete(json);
		if (pcStatusDecs->resultCode >= 400)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return false;
	}
}
bool JsonToStruct_Login_v1(char *pcJson, ST_IOTA_LOGIN_RES *pcStatusDecs)
{
	cJSON *json = NULL;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		pcStatusDecs->expires_in = cJSON_GetObjectItem(json, "expires_in")->valueint;
		pcStatusDecs->refresh_token = strdup(cJSON_GetObjectItem(json, "refresh_token")->valuestring);
		pcStatusDecs->access_token = strdup(cJSON_GetObjectItem(json, "access_token")->valuestring);
		pcStatusDecs->scope = strdup(cJSON_GetObjectItem(json, "scope")->valuestring);
		pcStatusDecs->token_type = strdup(cJSON_GetObjectItem(json, "token_type")->valuestring);
		pcStatusDecs->short_token = strdup(cJSON_GetObjectItem(json, "short_token")->valuestring);
		pcStatusDecs->client_id = strdup(cJSON_GetObjectItem(json, "client_id")->valuestring);

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}
/**
 * @description: 日志认证回执
 * @param {char} *pcJson
 * @param {ST_IOTA_AUTH_RES} *pcStatusDecs
 * @return {*}
 */
bool JsonToStruct_LoginAuth(char *pcJson, ST_IOTA_AUTH_RES *pcStatusDecs)
{
	cJSON *json = NULL;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		pcStatusDecs->accessKey = strdup(cJSON_GetObjectItem(json, "accessKey")->valuestring);
		pcStatusDecs->timestamp = strdup(cJSON_GetObjectItem(json, "timestamp")->valuestring);
		pcStatusDecs->nonce = strdup(cJSON_GetObjectItem(json, "nonce")->valuestring);
		pcStatusDecs->sign = strdup(cJSON_GetObjectItem(json, "sign")->valuestring);
		pcStatusDecs->url = strdup(cJSON_GetObjectItem(json, "url")->valuestring);
		pcStatusDecs->contentType = strdup(cJSON_GetObjectItem(json, "contentType")->valuestring);

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

/**
 * @description: 文件认证回执
 * @param {char} *pcJson
 * @param {ST_IOTA_FILE_RES} *pcStatusDecs
 * @return {*}
 */
bool JsonToStruct_FileRes(char *pcJson, ST_IOTA_FILE_RES *pcStatusDecs)
{
	cJSON *json = NULL;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		if (!cJSON_HasObjectItem(json, "resultCode"))
		{
			pcStatusDecs->resultCode = 200;
			pcStatusDecs->message = strdup("success");
			cJSON *data = cJSON_CreateObject();
			cJSON_AddStringToObject(data, "fileId", cJSON_GetObjectItem(json, "fileId")->valuestring);
			cJSON_AddNumberToObject(data, "fileSize", cJSON_GetObjectItem(json, "fileSize")->valueint);
			if (cJSON_HasObjectItem(json, "nodeCode"))
			{
				cJSON_AddStringToObject(data, "node", cJSON_GetObjectItem(json, "nodeCode")->valuestring);
			}
			else
			{
				cJSON_AddStringToObject(data, "node", "NULL");
			}
			if (cJSON_HasObjectItem(json, "zoneCode"))
			{
				cJSON_AddStringToObject(data, "zone", cJSON_GetObjectItem(json, "zoneCode")->valuestring);
			}
			else
			{
				cJSON_AddStringToObject(data, "zone", "NULL");
			}

			pcStatusDecs->data = cJSON_PrintUnformatted(data);
			cJSON_Delete(json);
			return true;
		}

		pcStatusDecs->resultCode = cJSON_GetObjectItem(json, "resultCode")->valueint;
		if (pcStatusDecs->resultCode == 200)
		{
			pcStatusDecs->data = cJSON_Print(cJSON_GetObjectItem(json, "data"));
		}
		pcStatusDecs->message = strdup(cJSON_GetObjectItem(json, "message")->valuestring);

		cJSON_Delete(json);
		if (pcStatusDecs->resultCode < 400)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

/**
 * @description: 文件上传返回data转换 目前只要fileid
 * @param {char} *pcJson
 * @param {ST_IOTA_FILE_DATA} *pcStatusDecs
 * @return {*}
 */
bool JsonToStruct_LogFileData(char *pcJson, ST_IOTA_FILE_DATA *pcStatusDecs)
{
	if (pcStatusDecs)
	{
		cJSON *json = NULL;
		json = cJSON_Parse(pcJson);
		pcStatusDecs->fileId = strdup(cJSON_GetObjectItem(json, "fileId")->valuestring);
		pcStatusDecs->fileSize = cJSON_GetObjectItem(json, "fileSize")->valueint;
		if (cJSON_HasObjectItem(json, "nodeCode"))
		{
			pcStatusDecs->node = strdup(cJSON_GetObjectItem(json, "nodeCode")->valuestring);
		}
		if (cJSON_HasObjectItem(json, "node"))
		{
			pcStatusDecs->node = strdup(cJSON_GetObjectItem(json, "node")->valuestring);
		}
		if (cJSON_HasObjectItem(json, "zoneCode"))
		{
			pcStatusDecs->zone = strdup(cJSON_GetObjectItem(json, "zoneCode")->valuestring);
		}
		if (cJSON_HasObjectItem(json, "zone"))
		{
			pcStatusDecs->zone = strdup(cJSON_GetObjectItem(json, "zone")->valuestring);
		}
		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

bool StructToJson_ReportToCenter(char *pcJson, ST_IOTA_REPORT_LOG_DATA *pcStatusDecs)
{
	cJSON *json = NULL;
	json = cJSON_CreateObject();
	if (json)
	{
		cJSON_AddStringToObject(json, "globalEventId", pcStatusDecs->globalEventId);
		cJSON_AddStringToObject(json, "reqNo", pcStatusDecs->reqNo);
		cJSON_AddStringToObject(json, "deviceSn", pcStatusDecs->deviceSn);
		cJSON_AddStringToObject(json, "deviceId", pcStatusDecs->deviceId);
		cJSON_AddStringToObject(json, "bussActTime", pcStatusDecs->bussActTime);
		if (pcStatusDecs->logActTime)
		{
			cJSON_AddStringToObject(json, "logActTime", pcStatusDecs->logActTime);
		}

		if (pcStatusDecs->bussId)
		{
			cJSON_AddStringToObject(json, "bussId", pcStatusDecs->bussId);
		}
		if (pcStatusDecs->bussName)
		{
			cJSON_AddStringToObject(json, "bussName", pcStatusDecs->bussName);
		}

		cJSON_AddStringToObject(json, "srvName", pcStatusDecs->srvName);
		cJSON_AddStringToObject(json, "moduleId", pcStatusDecs->moduleId);
		cJSON_AddStringToObject(json, "moduleName", pcStatusDecs->moduleName);
		cJSON_AddStringToObject(json, "logType", pcStatusDecs->logType);
		cJSON_AddStringToObject(json, "logTypeName", pcStatusDecs->logTypeName);
		if (pcStatusDecs->operUserId)
		{
			cJSON_AddStringToObject(json, "operUserId", pcStatusDecs->operUserId);
		}
		if (pcStatusDecs->operUserName)
		{
			cJSON_AddStringToObject(json, "operUserName", pcStatusDecs->operUserName);
		}
		if (pcStatusDecs->bussRetCode)
		{
			cJSON_AddStringToObject(json, "bussRetCode", pcStatusDecs->bussRetCode);
		}
		if (pcStatusDecs->bussRetMsg)
		{
			cJSON_AddStringToObject(json, "bussRetMsg", pcStatusDecs->bussRetMsg);
		}
		if (pcStatusDecs->className)
		{
			cJSON_AddStringToObject(json, "className", pcStatusDecs->className);
		}
		if (pcStatusDecs->methodName)
		{
			cJSON_AddStringToObject(json, "methodName", pcStatusDecs->methodName);
		}

		cJSON_AddStringToObject(json, "orgId", pcStatusDecs->orgId);

		cJSON_AddStringToObject(json, "orgName", pcStatusDecs->orgName);
		if (pcStatusDecs->clientIP)
		{
			cJSON_AddStringToObject(json, "clientIP", pcStatusDecs->clientIP);
		}
		cJSON_AddStringToObject(json, "srvIP", pcStatusDecs->srvIP);
		cJSON_AddStringToObject(json, "zone", pcStatusDecs->zone);
		cJSON_AddStringToObject(json, "node", pcStatusDecs->node);
		cJSON_AddStringToObject(json, "nodeName", pcStatusDecs->nodeName);
		if (pcStatusDecs->logInfo)
		{
			cJSON_AddStringToObject(json, "logInfo", pcStatusDecs->logInfo);
		}
		if (pcStatusDecs->domain)
		{
			cJSON_AddStringToObject(json, "domain", pcStatusDecs->domain);
		}
		if (pcStatusDecs->bussType)
		{
			cJSON_AddStringToObject(json, "bussType", pcStatusDecs->bussType);
		}
		*pcJson = cJSON_PrintUnformatted(json);
		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

bool StructToJson_ReportToSystem(char *pcJson, IOTA_SYSLOG *pcStatusDecs, int size)
{
	cJSON *array = cJSON_CreateArray();
	cJSON *json = cJSON_CreateObject();
	int i = 0;
	if (array)
	{
		while (i < size)
		{
			IOTA_SYSLOG device = pcStatusDecs[i];
			cJSON *devices = NULL;
			devices = cJSON_CreateObject();
			cJSON *services = NULL;
			services = cJSON_CreateObject();
			cJSON *data = NULL;
			data = cJSON_CreateObject();

			if (devices)
			{
				cJSON_AddStringToObject(devices, "deviceSn", device.deviceSn);
				if (services)
				{
					IOTA_SYSSVR *svr = &device.services;
					cJSON_AddStringToObject(services, "service", "sshMonitor");
					cJSON_AddStringToObject(services, "mid", svr->mid);
					cJSON_AddStringToObject(services, "eventTime", svr->eventTime);
					if (data)
					{
						IOTA_SYSDATA *data = &svr->data;
						cJSON_AddStringToObject(data, "user", data->user);
						cJSON_AddStringToObject(data, "srcIp", data->srcIp);
						cJSON_AddNumberToObject(data, "srcPort", data->srcPort);
						cJSON_AddStringToObject(data, "dstIp", data->dstIp);
						cJSON_AddNumberToObject(data, "dstIp", data->dstPort);
						cJSON_AddStringToObject(data, "loginTime", data->loginTime);
						cJSON_AddStringToObject(data, "opTime", data->opTime);
						cJSON_AddStringToObject(data, "cmd", data->cmd);
						cJSON_AddStringToObject(data, "cmdOutput", data->cmdOutput);
						cJSON_AddItemToObject(services, "data", data);
					}
				}
				cJSON_AddItemToObject(devices, "services", services);
				cJSON_AddItemToArray(array, devices);
			}

			i++;
		}

		if (json)
		{
			cJSON_AddItemToObject(json, "devices", array);
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

/**
 * @description:
 * @param {char} *pcJson
 * @param {ST_IOTA_LOGIN_RES} *pcStatusDecs
 * @return {*}
 */
int JsonToStruct_ROMACONF(char *pcJson, IOTA_ROMA_CONF_V1 *pcStatusDecs)
{
	cJSON *json = NULL;
	cJSON *data = NULL;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		if (cJSON_HasObjectItem(json, "resultCode"))
		{
			pcStatusDecs->resultCode = cJSON_GetObjectItem(json, "resultCode")->valueint;
		}
		else if (cJSON_HasObjectItem(json, "code"))
		{
			pcStatusDecs->resultCode = cJSON_GetObjectItem(json, "code")->valueint;
		}
		else
		{
			pcStatusDecs->resultCode = 400;
		}
		if (cJSON_HasObjectItem(json, "message"))
		{
			pcStatusDecs->message = strdup(cJSON_GetObjectItem(json, "message")->valuestring);
		}
		else if (cJSON_HasObjectItem(json, "msg"))
		{
			pcStatusDecs->message = strdup(cJSON_GetObjectItem(json, "msg")->valuestring);
		}
		else
		{
			pcStatusDecs->message = strdup("系统异常");
		}
		if (pcStatusDecs->resultCode < 400) // 服务端返回500时，data为null
		{
			if (cJSON_HasObjectItem(json, "data"))
			{
				data = cJSON_GetObjectItem(json, "data");
				if (data && !cJSON_IsNull(data))
				{
					if (!cJSON_HasObjectItem(data, "mqttIp"))
					{
						cJSON_Delete(json);
						return -1;
					}
					pcStatusDecs->mqttIp = strdup(cJSON_GetObjectItem(data, "mqttIp")->valuestring);
					if (cJSON_GetObjectItem(data, "mqttPort")->valuestring != NULL && strlen(cJSON_GetObjectItem(data, "mqttPort")->valuestring) != 0)
					{
						if (8443 == atoi(cJSON_GetObjectItem(data, "mqttPort")->valuestring))
						{
							pcStatusDecs->mqttPort = strdup("1883");
						}
						else if (18443 == atoi(cJSON_GetObjectItem(data, "mqttPort")->valuestring))
						{
							pcStatusDecs->mqttPort = strdup("11883");
						}
						else
						{
							pcStatusDecs->mqttPort = strdup(cJSON_GetObjectItem(data, "mqttPort")->valuestring);
						}
					}
					if (cJSON_GetObjectItem(data, "deviceId")->valuestring != NULL && strlen(cJSON_GetObjectItem(data, "deviceId")->valuestring) != 0)
					{
						pcStatusDecs->deviceId = strdup(cJSON_GetObjectItem(data, "deviceId")->valuestring);
					}
					if (cJSON_GetObjectItem(data, "deviceAuthSecret")->valuestring != NULL && strlen(cJSON_GetObjectItem(data, "deviceAuthSecret")->valuestring) != 0)
					{
						pcStatusDecs->deviceAuthSecret = strdup(cJSON_GetObjectItem(data, "deviceAuthSecret")->valuestring);
					}
					if (cJSON_GetObjectItem(data, "deviceAuthUser")->valuestring != NULL && strlen(cJSON_GetObjectItem(data, "deviceAuthUser")->valuestring) != 0)
					{
						pcStatusDecs->deviceAuthUser = strdup(cJSON_GetObjectItem(data, "deviceAuthUser")->valuestring);
					}
					if (cJSON_GetObjectItem(data, "mqttsIp")->valuestring != NULL && strlen(cJSON_GetObjectItem(data, "mqttsIp")->valuestring) != 0)
					{
						pcStatusDecs->mqttsIp = strdup(cJSON_GetObjectItem(data, "mqttsIp")->valuestring);
					}
					else
					{
						pcStatusDecs->mqttsIp = strdup(cJSON_GetObjectItem(data, "mqttIp")->valuestring);
					}

					if (cJSON_GetObjectItem(data, "mqttsPort")->valuestring != NULL && strlen(cJSON_GetObjectItem(data, "mqttsPort")->valuestring) != 0)
					{
						pcStatusDecs->mqttsPort = strdup(cJSON_GetObjectItem(data, "mqttsPort")->valuestring);
					}
					else
					{
						if (1883 == atoi(cJSON_GetObjectItem(data, "mqttPort")->valuestring))
						{
							pcStatusDecs->mqttsPort = strdup("8443");
						}
						else if (11883 == atoi(cJSON_GetObjectItem(data, "mqttPort")->valuestring))
						{
							pcStatusDecs->mqttsPort = strdup("18443");
						}
						else if (8443 == atoi(cJSON_GetObjectItem(data, "mqttPort")->valuestring))
						{
							pcStatusDecs->mqttsPort = strdup("8443");
						}
						else if (18443 == atoi(cJSON_GetObjectItem(data, "mqttPort")->valuestring))
						{
							pcStatusDecs->mqttsPort = strdup("18443");
						}
					}

					if (cJSON_HasObjectItem(data, "TLS"))
					{
						pcStatusDecs->tls = cJSON_GetObjectItem(data, "TLS")->valueint;
					}
					
				}
				else
				{
					cJSON_Delete(json);
					return -1;
				}
				
			}
			else
			{
				cJSON_Delete(json);
				return -1;
			}
		}
		if (cJSON_HasObjectItem(json, "timeStamp"))
		{
			pcStatusDecs->timeStamp = cJSON_GetObjectItem(json, "timeStamp")->valuedouble;
		}
		cJSON_Delete(json);
		if (pcStatusDecs->resultCode >= 400)
		{
			return -1;
		}
		else
		{
			return 0;
		}
	}
	else
	{
		return -2;
	}
}

bool StructToJson_WeakPasswdRes(char **pcJson, ST_IOTA_WEAKPASSWD *pcStatusDecs)
{
	dzlog_info("StructToJson_WeakPasswdRes start 00");
	if (pcStatusDecs)
	{
		cJSON *json = NULL;
		cJSON *devs = NULL;
		cJSON *dev = NULL;
		json = cJSON_CreateObject();
		char cTime[32] = {0};
		int ret = systemDate2(cTime);
		// json
		cJSON_AddStringToObject(json, "service", "weakPasswdReport");
		cJSON_AddNumberToObject(json, "code", pcStatusDecs->code);
		cJSON_AddStringToObject(json, "deviceSn", pcStatusDecs->deviceSn);
		cJSON_AddStringToObject(json, "deviceId", pcStatusDecs->deviceId);
		cJSON_AddStringToObject(json, "msgTime", cTime);
		if (pcStatusDecs->code == 200)
		{
			ST_IOTA_LOG_BODY *st_body = (ST_IOTA_LOG_BODY *)pcStatusDecs->params;
			if (st_body)
			{
				cJSON_AddItemToObject(json, "params", devs = cJSON_CreateArray());
				cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());
				cJSON_AddStringToObject(dev, "filename", st_body->FileName);
				cJSON_AddStringToObject(dev, "fileId", st_body->filed);
				cJSON_AddStringToObject(dev, "md5", st_body->md5);
				cJSON_AddNumberToObject(dev, "size", st_body->size);
			}
		}
		*pcJson = cJSON_PrintUnformatted(json);
		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

int JsonToStruct_FILECONF(char *pcJson, IOTA_FILE_CONF_V1 *pcStatusDecs)
{
	cJSON *json = NULL;
	cJSON *data = NULL;
	json = cJSON_Parse(pcJson);
	if (json)
	{
		if (!cJSON_HasObjectItem(json, "resultCode"))
		{
			return -2;
		}
		pcStatusDecs->resultCode = cJSON_GetObjectItem(json, "resultCode")->valueint;
		pcStatusDecs->message = strdup(cJSON_GetObjectItem(json, "message")->valuestring);
		if (pcStatusDecs->resultCode < 400) // 服务端返回500时，data为null
		{
			if (cJSON_HasObjectItem(json, "data"))
			{
				data = cJSON_GetObjectItem(json, "data");
				pcStatusDecs->appId = strdup(cJSON_GetObjectItem(data, "appId")->valuestring);
				pcStatusDecs->appKey = strdup(cJSON_GetObjectItem(data, "appKey")->valuestring);
				pcStatusDecs->uploadFileUrl = strdup(cJSON_GetObjectItem(data, "uploadFileUrl")->valuestring);
				pcStatusDecs->downloadFileUrl = strdup(cJSON_GetObjectItem(data, "downloadFileUrl")->valuestring);
				dzlog_debug("JsonToStruct_FILECONF start 55");
			}
		}

		cJSON_Delete(json);
		if (pcStatusDecs->resultCode >= 400)
		{
			return -1;
		}
		else
		{
			return 0;
		}
	}
	else
	{
		return -2;
	}
}

/**
 * @description: 结构体转换，网络策略响应上报
 * @param {char} * 传出json
 * @param {ST_IOTA_LOG_RES} *pcStatusDecs 传入结构体
 * @return {*} 是否成功
 */
bool StructToJson_NETWORKLIST(char **pcJson, ST_IOTA_LOG_RES *pcStatusDecs, int num, PIOTA_NETWORKLIST_PARAM_V1 inputpar)
{
	if (pcStatusDecs)
	{
		cJSON *json = NULL;
		cJSON *infos = NULL;
		cJSON *info = NULL;
		json = cJSON_CreateObject();
		char cTime[128] = {0};
		int ret = systemDate2(cTime);
		// json
		cJSON_AddStringToObject(json, "mid", pcStatusDecs->iMid);
		cJSON_AddNumberToObject(json, "code", pcStatusDecs->code);
		cJSON_AddStringToObject(json, "deviceSn", pcStatusDecs->deviceSn);
		cJSON_AddStringToObject(json, "replyId", pcStatusDecs->replyId);
		cJSON_AddStringToObject(json, "zone", pcStatusDecs->zone);
		cJSON_AddStringToObject(json, "node", pcStatusDecs->node);
		cJSON_AddStringToObject(json, "msgTime", cTime);
		cJSON_AddStringToObject(json, "deviceId", pcStatusDecs->deviceID);
		cJSON_AddItemToObject(json, "params", infos = cJSON_CreateArray());
		if (inputpar)
		{
			int i = 0;
			for (; i < num; i++)
			{
				cJSON_AddItemToArray(infos, info = cJSON_CreateObject());
				cJSON_AddStringToObject(info, "policyId", inputpar->policyId);
				cJSON_AddStringToObject(info, "permissionStatus", inputpar->permissionStatus);
				cJSON_AddStringToObject(info, "srcIp", inputpar->srcIp);
				cJSON_AddStringToObject(info, "srcPort", inputpar->srcPort);
				cJSON_AddStringToObject(info, "protocol", inputpar->protocol);
				inputpar++;
			}
		}
		*pcJson = cJSON_PrintUnformatted(json);

		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}

bool StructToJson_DictionaryDetele(char **pcJson, ST_IOTA_LOG_RES *pcStatusDecs)
{
    if (pcStatusDecs)
    {
		cJSON *json = NULL;
    	// cJSON *body = NULL;
    	json = cJSON_CreateObject();
    	// body = cJSON_CreateArray();
    	char cTime[128] = {0};
    	int ret = systemDate2(cTime);
        // json
        cJSON_AddStringToObject(json, "mid", pcStatusDecs->iMid);
        cJSON_AddStringToObject(json, "deviceSn", pcStatusDecs->deviceSn);
        cJSON_AddNumberToObject(json, "code", pcStatusDecs->code);
        cJSON_AddStringToObject(json, "replyId", pcStatusDecs->replyId);
        cJSON_AddStringToObject(json, "zone", pcStatusDecs->zone);
        cJSON_AddStringToObject(json, "node", pcStatusDecs->node);
        cJSON_AddStringToObject(json, "msgTime", cTime);
        cJSON_AddStringToObject(json, "deviceId", pcStatusDecs->deviceID);

        // body
        // IOTA_DICTIONARY_INFO *st_body = (IOTA_DICTIONARY_INFO *)pcStatusDecs->body;
		// cJSON_AddStringToObject(json, "body", st_body);
        // cJSON_AddItemToObject(json, "body", body);
        *pcJson = cJSON_PrintUnformatted(json);

        cJSON_Delete(json);
        return true;
    }
    else
    {
        return false;
    }
}

char* extract_value_v1(const char* line) {
    char* colon = strchr(line, ':');
    if (!colon) return NULL;
    
    char* value_start = colon + 1;
    while (*value_start == ' ' || *value_start == '\t') value_start++;
    
    char* value = strdup(value_start);
    char* newline = strchr(value, '\n');
    if (newline) *newline = '\0';
    
    return value;
}

bool StructToJson_policy_video_info(char **pcJson, ST_IOTA_LOG_RES *pcStatusDecs,cJSON* video_json)
{
	dzlog_info("StructToJson_policy_video_info");
    if (pcStatusDecs)
    {
		cJSON *json = NULL;
    	cJSON *body = NULL;
    	json = cJSON_CreateObject();
    	char cTime[128] = {0};
    	int ret = systemDate2(cTime);
        // json
        cJSON_AddStringToObject(json, "mid", pcStatusDecs->iMid);
        cJSON_AddStringToObject(json, "deviceSn", pcStatusDecs->deviceSn);
        cJSON_AddNumberToObject(json, "code", pcStatusDecs->code);
        cJSON_AddStringToObject(json, "replyId", pcStatusDecs->replyId);
        cJSON_AddStringToObject(json, "zone", pcStatusDecs->zone);
        cJSON_AddStringToObject(json, "node", pcStatusDecs->node);
        cJSON_AddStringToObject(json, "msgTime", cTime);
        cJSON_AddStringToObject(json, "deviceId", pcStatusDecs->deviceID);
        // 转result
		
		if(video_json!=NULL)
		{

			POLICY_SECURITY_PARAM st_body ={0};
			cJSON* item = cJSON_GetObjectItem(video_json, "forward_dev");
			if (item && cJSON_IsString(item) ) strncpy(st_body.forwardDev, item->valuestring, sizeof(st_body.forwardDev) - 1);
			
			item = cJSON_GetObjectItem(video_json, "ip_type");
			if (item &&cJSON_IsString(item)) strncpy(st_body.ipType, item->valuestring, sizeof(st_body.ipType)-1);
			
			item = cJSON_GetObjectItem(video_json, "video_platform_ip");
			if (item &&cJSON_IsString(item)) strncpy(st_body.videoPlatformIp, item->valuestring, sizeof(st_body.videoPlatformIp)-1);
			
			item = cJSON_GetObjectItem(video_json, "forward_status");
			if (item &&cJSON_IsString(item)) strncpy(st_body.forwardStatus, item->valuestring, sizeof(st_body.forwardStatus)-1);
			
			item = cJSON_GetObjectItem(video_json, "video_platform_masklen");
			if (item &&cJSON_IsString(item)) strncpy(st_body.videoPlatformMasklen, item->valuestring, sizeof(st_body.videoPlatformMasklen)-1);
			
			item = cJSON_GetObjectItem(video_json, "video_gateway");
			if (item &&cJSON_IsString(item)) strncpy(st_body.videoGateway, item->valuestring, sizeof(st_body.videoGateway)-1);

			cJSON_AddItemToObject(json, "body", body = cJSON_CreateObject());
			cJSON_AddStringToObject(body, "forwardDev", st_body.forwardDev);
			cJSON_AddStringToObject(body, "ipType", st_body.ipType);
			cJSON_AddStringToObject(body, "videoPlatformIp", st_body.videoPlatformIp);
			cJSON_AddStringToObject(body, "forwardStatus", st_body.forwardStatus);
			cJSON_AddStringToObject(body, "videoPlatformMasklen", st_body.videoPlatformMasklen);
			cJSON_AddStringToObject(body, "videoGateway", st_body.videoGateway);
		}
        *pcJson = cJSON_PrintUnformatted(json);
        cJSON_Delete(json);
        return true;
    }
    else
    {
        return false;
    }	
}

bool StructToJson_PolicySecurity_Info(char **pcJson, ST_IOTA_LOG_RES *pcStatusDecs,cJSON* info_json)
{
    if (pcStatusDecs)
    {
		cJSON *json = NULL;
		cJSON *body = NULL;
		cJSON *portList = NULL;
		json = cJSON_CreateObject();

		char cTime[128] = {0};
    	int ret = systemDate2(cTime);
        // json
        cJSON_AddStringToObject(json, "mid", pcStatusDecs->iMid);
        cJSON_AddStringToObject(json, "deviceSn", pcStatusDecs->deviceSn);
        cJSON_AddNumberToObject(json, "code", pcStatusDecs->code);
        cJSON_AddStringToObject(json, "replyId", pcStatusDecs->replyId);
        cJSON_AddStringToObject(json, "zone", pcStatusDecs->zone);
        cJSON_AddStringToObject(json, "node", pcStatusDecs->node);
        cJSON_AddStringToObject(json, "msgTime", cTime);
        cJSON_AddStringToObject(json, "deviceId", pcStatusDecs->deviceID);
		if(info_json)
		{
			char SshServer[10]; //ssh的延迟时长
    		char MqttPort[10]; //mqtt的延迟时长
			cJSON* item = cJSON_GetObjectItem(info_json, "SshServer");
			if (cJSON_IsString(item)) strncpy(SshServer, item->valuestring, sizeof(SshServer));
			item = cJSON_GetObjectItem(info_json, "MqttPort");
			if (cJSON_IsString(item)) strncpy(MqttPort, item->valuestring, sizeof(MqttPort));

			cJSON_AddItemToObject(json, "body", body = cJSON_CreateObject());
			portList = cJSON_CreateArray();

			// cJSON_ArrayForEach(item, info_json)
			// {
			// 	const char *key = item->string;
			// 	if (key && strncmp(key, "Port", 4) == 0 && cJSON_IsString(item))
			// 	{
			// 		// 提取端口号部分（去掉"Port"前缀）
			// 		const char *portStr = key + 4;
			// 		// 转换为整数端口号（自动去除前导零）
			// 		int port = atoi(portStr);
			// 		if (port > 0) // 确保端口号有效
			// 		{
			// 			// 添加端口键值对（端口号作为键，值转换为整数）
			// 			char portNum[6] = {0};
			// 			sprintf(portNum, "%d", port);
			// 			cJSON_AddNumberToObject(portList, portNum, atoi(item->valuestring));
			// 		}
			// 	}
			// }
			if (cJSON_HasObjectItem(info_json, "Port"))
			{
				cJSON* portItem = cJSON_GetObjectItem(info_json, "Port"); 
				if (cJSON_IsObject(portItem))  // 改为判断是否为JSON对象
				{
					cJSON* child = NULL;
					int i = 0;
					child = portItem->child;  
					while (child != NULL && i < 100)  // 假设portList最大支持100个端口，可根据实际调整
					{
						// 键为端口号字符串（如"8801"），转换为整数
						cJSON *portObj = cJSON_CreateObject();
						// cJSON_AddNumberToObject(portList, child->string, atoi(child->valuestring));
						// 值为Change状态（如21）
						// st_param->portList[i].time = child->valueint;
						
						cJSON_AddNumberToObject(portObj, "port", atoi(child->string));
						cJSON_AddNumberToObject(portObj, "time", atoi(child->valuestring));
						cJSON_AddItemToArray(portList, portObj);
						dzlog_info("port: %s, time: %s", 
								child->string, 
								child->valuestring);
						
						child = child->next;    // 迭代下一个键值对
						i++;

					}
				}
			}  
			cJSON_AddItemToObject(body, "portList", portList);
			cJSON_AddNumberToObject(body, "ssh", atoi(SshServer));
			cJSON_AddNumberToObject(body, "mqtt", atoi(MqttPort));
		}
        *pcJson = cJSON_PrintUnformatted(json);

        cJSON_Delete(json);
        return true;
    }
    return false;
}

bool StructToJson_firewall(char** pcJson, ST_IOTA_LOG_RES* pcStatusDecs, RuleCollection* db_ipt_entries, char* des)
{
	if (pcStatusDecs)
	{
		
		cJSON* json = NULL;
		json = cJSON_CreateObject();
		char cTime[64] = { 0 };
		int ret = systemDate4(cTime);
		cJSON_AddStringToObject(json, "mid", pcStatusDecs->iMid);
		cJSON_AddNumberToObject(json, "code", pcStatusDecs->code);
		cJSON_AddStringToObject(json, "deviceSn", pcStatusDecs->deviceSn);
		cJSON_AddStringToObject(json, "replyId", pcStatusDecs->replyId);
		cJSON_AddStringToObject(json, "zone", pcStatusDecs->zone);
		cJSON_AddStringToObject(json, "node", pcStatusDecs->node);
		cJSON_AddStringToObject(json, "msgTime", cTime);
		cJSON_AddStringToObject(json, "deviceId", pcStatusDecs->deviceID);
		if (des)
		{
			cJSON_AddStringToObject(json, "msg", des);
		}
		if (db_ipt_entries)
		{
			if (db_ipt_entries->count > 0)
			{
				cJSON* devs = NULL;
				cJSON* dev = NULL;
				cJSON_AddItemToObject(json, "body", devs = cJSON_CreateArray());
				int i = 0;
				for (i = 0; i < db_ipt_entries->count; i++)
				{
					cJSON_AddItemToArray(devs, dev = cJSON_CreateObject());
					cJSON_AddStringToObject(dev, "policyId", db_ipt_entries->rules->policyId);
					cJSON_AddStringToObject(dev, "chainDirection", db_ipt_entries->rules->chainDirection);
					char tmp[16] = { 0 };
					sprintf(tmp, "%d", db_ipt_entries->rules->prio);
					cJSON_AddStringToObject(dev, "prio", tmp);
					cJSON_AddStringToObject(dev, "permissionStatus", db_ipt_entries->rules->permissionStatus);
					cJSON_AddStringToObject(dev, "protocol", db_ipt_entries->rules->protocol);
					cJSON_AddStringToObject(dev, "srcIp", db_ipt_entries->rules->srcIp);
					if (db_ipt_entries->rules->srcPort != NULL && strlen(db_ipt_entries->rules->srcPort) > 0)
					{
						cJSON_AddStringToObject(dev, "srcPort", db_ipt_entries->rules->srcPort);
					}

					cJSON_AddStringToObject(dev, "dstIp", db_ipt_entries->rules->dstIp);

					if (db_ipt_entries->rules->dstPort != NULL && strlen(db_ipt_entries->rules->dstPort) > 0)
					{
						cJSON_AddStringToObject(dev, "dstPort", db_ipt_entries->rules->dstPort);
					}

					cJSON_AddStringToObject(dev, "state", db_ipt_entries->rules->state);
					memset(tmp, 0, 16);
					sprintf(tmp, "%d", db_ipt_entries->rules->ruleOrigin);
					cJSON_AddStringToObject(dev, "ruleOrigin", tmp);

					db_ipt_entries->rules++;
				}
			}
		}
		*pcJson = cJSON_PrintUnformatted(json);
		cJSON_Delete(json);
		return true;
	}
	else
	{
		return false;
	}
}
